local snow = require("meet_your_mods.mhr").snow
local via  = require("meet_your_mods.mhr").via

--村区域
snow.stage.StageDef.AreaNoType:fetch_enum("none", "None")
snow.stage.StageDef.AreaNoType:fetch_enum("village", "No00")
snow.stage.StageDef.AreaNoType:fetch_enum("village_home", "No01")
snow.stage.StageDef.AreaNoType:fetch_enum("otomo_square", "No02")
snow.stage.StageDef.AreaNoType:fetch_enum("hub", "No03")
snow.stage.StageDef.AreaNoType:fetch_enum("preparing_area", "No04")
snow.stage.StageDef.AreaNoType:fetch_enum("training_area", "No05")
snow.stage.StageDef.AreaNoType:fetch_enum("plaza", "No06")
snow.stage.StageDef.AreaNoType:fetch_enum("plaza_home", "No07")

--地图区域
snow.QuestMapManager.MapNoType:fetch_enum("none", "None")
snow.QuestMapManager.MapNoType:fetch_enum("shrine_ruins", "No01")
snow.QuestMapManager.MapNoType:fetch_enum("sandy_plains", "No02")
snow.QuestMapManager.MapNoType:fetch_enum("flooded_forest", "No03")
snow.QuestMapManager.MapNoType:fetch_enum("frost_island", "No04")
snow.QuestMapManager.MapNoType:fetch_enum("lava_caverns", "No05")
snow.QuestMapManager.MapNoType:fetch_enum("jungle", "No31")
snow.QuestMapManager.MapNoType:fetch_enum("citadel", "No32")
snow.QuestMapManager.MapNoType:fetch_enum("infernal_springs", "No09")
snow.QuestMapManager.MapNoType:fetch_enum("arena", "No10")
snow.QuestMapManager.MapNoType:fetch_enum("coral_palace", "No11")
snow.QuestMapManager.MapNoType:fetch_enum("forlorn_arena", "No41")
snow.QuestMapManager.MapNoType:fetch_enum("yawning_abyss", "No42")

local status = {
    logical_status = {
        none         = 0,
        village      = 1,
        training     = 2,
        quest        = 3,
        quest_ending = 4,
    },
    languages = {
        default              = via.Language.English,
        japanese             = via.Language.Japanese,
        english              = via.Language.English,
        french               = via.Language.French,
        italian              = via.Language.Italian,
        german               = via.Language.German,
        spanish              = via.Language.Spanish,
        russian              = via.Language.Russian,
        polish               = via.Language.Polish,
        portuguese_brasilian = via.Language.PortugueseBr,
        korean               = via.Language.Korean,
        chinese_troditional  = via.Language.TransitionalChinese,
        chinese_simplified   = via.Language.SimplelifiedChinese,
        arabic               = via.Language.Arabic,
    },
    language = nil,
}

function status:display_language()
    return self.language or self.languages.default
end

function status:update_display_language()
    local language = snow.gui.OptionManager:get_singleton()
        and snow.gui.OptionManager:get_singleton():getDisplayLanguage()
        or self.languages.default
    self.language = language
end

function status:post_setDispLanguage(result)
    self:update_display_language()
    return result
end

function status:post_set_Language(result)
    self:update_display_language()
    return result
end

function status:current_status()
    local game_manager = snow.SnowGameManager:get_singleton()
    if not game_manager then
        return self.logical_status.none
    end

    local game_status = game_manager:get_CurrentStatus()
    if snow.SnowGameManager.StatusType.Village == game_status then
        return snow.VillageAreaManager:checkCurrentArea_TrainingArea()
            and self.logical_status.training or self.logical_status.village
    elseif snow.SnowGameManager.StatusType.Quest == game_status then
        local quest_manager = snow.QuestManager:get_singleton()
        if not quest_manager then
            return self.logical_status.none
        end
        if snow.QuestManager.Status.Play == quest_manager:getStatus() then
            return self.logical_status.quest
        end
        return self.logical_status.quest_ending
    end

    return self.logical_status.none
end

function status:current_area()
    local game_manager = snow.SnowGameManager:get_singleton()

    return game_manager
        and snow.SnowGameManager.StatusType.Village == game_manager:get_CurrentStatus()
        -- VillageAreaManager会在进入任务时销毁，并在回到村庄时重新构造
        and snow.VillageAreaManager:get_singleton(true):getCurrentAreaNo()
        or snow.stage.StageDef.AreaNoType.none
end

function status:current_map()
    if snow.SnowGameManager.StatusType.Quest == snow.SnowGameManager:get_singleton():get_CurrentStatus() then
        return snow.QuestManager:get_singleton():getMapNo()
    end
    return snow.QuestMapManager.MapNoType.none
end

function status:current_position()
    local game_status = snow.SnowGameManager:get_singleton():get_CurrentStatus()
    if game_status < snow.SnowGameManager.StatusType.Village
        or game_status > snow.SnowGameManager.StatusType.Result
    then
        return nil
    end
    return snow.player.PlayerManager:get_singleton():findMasterPlayer():get_GameObject():get_Transform():get_Position()
end

function status:quest_activated()
    return snow.QuestManager:get_singleton():getActiveQuestData() ~= nil
end

function status:quest_ending_flow()
    local quest_manager = snow.QuestManager:get_singleton()
    if snow.SnowGameManager.StatusType.Quest == snow.SnowGameManager:get_singleton():get_CurrentStatus()
        and quest_manager:getStatus() ~= snow.QuestManager.Status.Play
    then
        return quest_manager._EndFlow
    end
    return snow.QuestManager.EndFlow.None
end

function status:current_save_data()
    local save_service = snow.SnowSaveService:get_singleton()
    if not save_service
        or not save_service:getSlotNo()
        or -1 == save_service:getSlotNo()
    then
        return nil
    end
    return save_service:getSlotNo()
end

function status:is_leisure()
    local status = self:current_status()
    return self.logical_status.village == status
        or self.logical_status.quest_ending == status
        or self.logical_status.quest == status
        and snow.quest.QuestType.TOUR == snow.QuestManager:get_singleton()._QuestType & snow.quest.QuestType.TOUR
        and not self:is_fighting()
end

function status:is_fighting()
    return snow.SnowSaveService:get_singleton()._PlayingFight
end

function status:is_online()
    return snow.LobbyManager:get_singleton():isOnline()
end

function status:gui_opening()
    local gui = snow.gui.GuiManager:get_singleton()
    return self:current_status() ~= self.logical_status.village
        or gui:IsStartMenuAndSubmenuOpen()
        or gui:isSensorAccess()
        or gui:isCallingOwl()
        or gui:isOpenOwlMenu()
        or gui:isOpenGuiQuestBoard()
        or gui:isOpenHud()
        or gui:isInvisibleAllGUI()
        or snow.NowLoading:get_singleton():get_isBusy()
end

function status:get_delta_time()
    return via.Application:get_FrameTimeMillisecond()
end

function status:initialize()
    --更新用户语言设置
    snow.gui.OptionManager.setDispLanguage:sdk_auto_hook(self)
    via.ResourceManager.set_Language:sdk_auto_hook(self)
    self:update_display_language()
end

return status
